#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
エクスポート アイテム バリデート ツールキット機能

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# ------------------------------------------------------------
# Python

# ESiPC
from esipc.resource.iniresource import ESiPCINIFileResourceBundle

# Mikan
from mikan.idt import AbstractMikanItemDBToolkit
from mikan.core import MIKAN
from mikan.exception.item import MikanRequiredItemException

# ------------------------------------------------------------
## 変数
# ------------------------------------------------------------
RESOURCE = ESiPCINIFileResourceBundle(__file__)

# ------------------------------------------------------------
## 関数
# ------------------------------------------------------------
def _(key, default, locale=None):
	try:
		return RESOURCE.get_string(key, default, locale)
	except:
		return default

# ------------------------------------------------------------
## クラス
# ------------------------------------------------------------
class MIKAN_IDTVALIDATE(MIKAN):
	pass

class AbstractMikanValidateIDT(AbstractMikanItemDBToolkit, MIKAN_IDTVALIDATE):
	def __init__(self, itemdb):
		super(AbstractMikanValidateIDT, self).__init__(itemdb)

	def validate(self, entity, form=None):
		key_id = None
		try:
			key_id = entity.getIDAttribute().definename
		except:
			pass
		key_create = None
		try:
			key_create = entity.getCreateDateAttribute().definename
		except:
			pass
		key_lastup = None
		try:
			key_lastup = entity.getLastUpDateAttribute().definename
		except:
			pass

		es = []
		for k, v in entity.attributes.items():
			if k != key_id:
				if v.isrequired:
					if k != key_create:
						if k != key_lastup:
							if entity.values[k] == None:
								es.append(MikanRequiredItemException(self.request, v))

		if len(es):
			return es
		return None

	def validates(self, entities, form=None):
		es = []
		for e in entities:
			ex = self.validate(e, form)
			if ex:
				es.append(ex)

		if len(es):
			return es
		return None

class MikanValidateIDT(AbstractMikanValidateIDT):
	def __init__(self, itemdb):
		super(MikanValidateIDT, self).__init__(itemdb)

# ------------------------------------------------------------
## メイン関数
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
